// pages/webview.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    i: 0,
    is_open: false
  },
  startRecord: function () {
    let that = this;
    this.setData({
      i: 0,
      is_open: true
    });
    that.updatePercent();
    this.cameraContext.startRecord({
      success: function () {
      },

    })
  },
  updatePercent: function () {
    let that = this;
    const is_open = this.data.is_open;
    setTimeout(function () {
      var i = that.data.i;
      i++;
      that.setData({
        i: i
      })
      if (i < 10 && is_open) {
        that.updatePercent();
      } else {
        that.stopRecord();
      }
    }, 1000);
  },
  stopRecord: function () {
    var that = this;
    that.setData({
      is_open: false
    });
    wx.showLoading({
      title: '生成视频中……',
    })
    this.cameraContext.stopRecord({
      success: function (res) {
        wx.hideLoading();
        wx.navigateTo({
          url: 'preview?path=' + res.tempVideoPath,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.cameraContext = wx.createCameraContext();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
})